package morenoapp.logging;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import morenoapp.components.common.ExceptionDialog;
import morenoapp.net.MorenoNet;
import morenoapp.util.MorenoUtil;

/**
 *
 * @author dmitry.mamonov
 */
public class MorenoLog {
    private static final Logger exceptionLogger = Logger.getLogger("morena.exceptions");

    public static void configureLogging() {
        //remove old handlers
        for (Handler handler : Logger.getLogger("").getHandlers()) {
            Logger.getLogger("").removeHandler(handler);
        }

        //custom formatter
        Formatter formatter = new Formatter() {
            // Line separator string.  This is the value of the line.separator
            // property at the moment that the SimpleFormatter was created.
            private String lineSeparator = System.getProperty("line.separator");
            @Override
            public String format(LogRecord lr) {
                //thread,class,method,time, message
                //excaption
                String tail = lineSeparator;

                return String.format("[%6s|%3d|%-40s|%-30s|%15d] %s%s%s%s",
                        lr.getLevel().getName(),
                        lr.getThreadID(),
                        lr.getSourceClassName() != null ? lr.getSourceClassName() : lr.getLoggerName(),
                        lr.getSourceMethodName(),
                        lr.getMillis(),
                        lr.getMessage(),
                        lineSeparator,
                        lr.getThrown() != null ? MorenoUtil.toStirng(lr.getThrown()) : "",
                        lr.getThrown() != null ? lineSeparator : "");
            }

        };

        try {
            File logsDir = new File("logs");
            if (logsDir.exists() == false) {
                logsDir.mkdirs();
            }
            FileHandler fileHandler = new FileHandler("logs/morena-log-%g.txt", 1000 * 1000, 3);
            fileHandler.setFormatter(formatter);
            Logger.getLogger("").addHandler(fileHandler);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (SecurityException ex) {
            ex.printStackTrace();
        }

        if (MorenoUtil.isDebug()) {
            ConsoleHandler consoleHandler = new ConsoleHandler() {
                {
                    setOutputStream(System.out);
                }

            };

            consoleHandler.setFormatter(new Formatter() {
                @Override
                public String format(LogRecord record) {
                    String trace = "";
                    if (record.getThrown() != null) {
                        trace = MorenoUtil.toStirng(record.getThrown());
                    }
                    return String.format("%s | %s\n%s", record.getLevel().getName(), record.getMessage(), trace);
                }

            });
            Logger.getLogger("").addHandler(consoleHandler);
        }
    }


    public static void notifyUnknownException(Throwable _th){
        exceptionLogger.log(Level.SEVERE, null, _th);
        ExceptionDialog.showDialog(_th);
        MorenoNet.createIssue(_th);
    }
    public static void notifyException(String _message, Throwable _th){
        exceptionLogger.log(Level.INFO, _message, _th);
        ExceptionDialog.showDialog(_message, _th);
        MorenoNet.createIssue(_th);
    }
    public static void warn(Throwable _th) {
        exceptionLogger.log(Level.WARNING, null, _th);
        MorenoNet.createIssue(_th);
    }
}
